<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ApiResource()
 * @ODM\Document(repositoryClass="App\Repository\AdminMenuRepository")
 */
class AdminMenu
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ODM\Id(strategy="INCREMENT")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Common(cnName="名称",listType="text",editType="text")
     * @ODM\Field(type="string")
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @Common(cnName="链接",listType="text",editType="text")
     * @ODM\Field(type="string",nullable=true)
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $url;

    /**
     * @Common(cnName="图标",listType="text",editType="text")
     * @ODM\Field(type="string",nullable=true)
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $icon;

    /**
     * @Common(cnName="排序",listType="text",editType="text")
     * @ODM\Field(type="integer",nullable=true)
     * @ORM\Column(type="integer", nullable=true)
     */
    private $sort;

    /**
     * @Common(cnName="父栏目",listType="text",editType="select")
     * @ODM\ReferenceOne(targetDocument="App\Entity\AdminMenu", inversedBy="sons",nullable=true)
     * @ORM\ManyToOne(targetEntity="App\Entity\AdminMenu", inversedBy="sons")
     */
    private $parent;

    /**
     * @Common(cnName="子栏目",listType="text",editType="text")
     * @ODM\ReferenceMany(targetDocument="App\Entity\AdminMenu", mappedBy="parent",sort={"sort"="ASC"})
     * @ORM\OneToMany(targetEntity="App\Entity\AdminMenu", mappedBy="parent")
     * @ORM\OrderBy({"sort"="ASC"})
     */
    private $sons;

    /**
     * @Common(cnName="权限分组",listType="text",editType="text")
     * @ODM\ReferenceMany(targetDocument="App\Entity\AdminGroup", mappedBy="menus")
     * @ORM\ManyToMany(targetEntity="App\Entity\AdminGroup", mappedBy="menus")
     */
    private $groups;

    public function __construct()
    {
        $this->sons = new ArrayCollection();
        $this->groups = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url): self
    {
        $this->url = $url;

        return $this;
    }

    public function getIcon(): ?string
    {
        return $this->icon;
    }

    public function setIcon(?string $icon): self
    {
        $this->icon = $icon;

        return $this;
    }

    public function getSort(): ?int
    {
        return $this->sort;
    }

    public function setSort(?int $sort): self
    {
        $this->sort = $sort;

        return $this;
    }

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|self[]
     */
    public function getSons(): Collection
    {
        return $this->sons;
    }

    public function addSon(self $son): self
    {
        if (!$this->sons->contains($son)) {
            $this->sons[] = $son;
            $son->setParent($this);
        }

        return $this;
    }

    public function removeSon(self $son): self
    {
        if ($this->sons->contains($son)) {
            $this->sons->removeElement($son);
            // set the owning side to null (unless already changed)
            if ($son->getParent() === $this) {
                $son->setParent(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|AdminGroup[]
     */
    public function getGroups(): Collection
    {
        return $this->groups;
    }

    public function addGroup(AdminGroup $group): self
    {
        if (!$this->groups->contains($group)) {
            $this->groups[] = $group;
            $group->addMenu($this);
        }

        return $this;
    }

    public function removeGroup(AdminGroup $group): self
    {
        if ($this->groups->contains($group)) {
            $this->groups->removeElement($group);
            $group->removeMenu($this);
        }

        return $this;
    }
}
